var index = require('../index/index.js')
Page({
  data: {
    firstIndex: -1,
    quan: 1,
    isSelect: 0,
    colorItems: [],
    specItems: [],
    attr: [],
    total: 0,
    indexData: '',
    dis: 0
  },

  addQuan: function (e) {
    var quan = this.data.quan
    this.setData({
      quan: quan + 1
    })
    this.setData({
      total: this.data.quan * this.data.prod.price
    })
  },
  minuQuan: function (e) {
    var quan = this.data.quan
    if (quan > 1) {
      this.setData({
        quan: quan - 1
      })
    }
    this.setData({
      total: this.data.quan * this.data.prod.price
    })
  },
  beSelect: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      dataid: e.currentTarget.dataset.id,
      dataclass: e.currentTarget.dataset.class
    });
    console.log(e.currentTarget.dataset.class)
    // console.log(e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var dis = 0
    console.log("1111:" + index.data.userInfo.pid)
    // console.log(index.data)
    if (index.data.userInfo.pid == undefined) {
      console.log("1111:" + index.data.userInfo.pid)
      dis = 0
    }
    else {
      dis = index.data.userInfo.discount
    }
    that.setData({
      indexData: index.data,
      dis: dis
    })
    console.log(index.data)
    wx.request({
      url: 'https://selwyn.mynatapp.cc/order/findDiscount', //仅为示例，并非真实的接口地址
      // data: {
      //   id: that.data.prodId
      // },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'post',
      success: function (res) {

        console.log(res.data[0].discount)
        that.setData({
          discount: res.data[0].discount
        })
      }
    })
    this.setData({
      prodId: options.id
    })
    var prod = JSON.parse(options.prod)
    this.setData({
      prod: prod
    })
    console.log(this.data.prod)
    var that = this
    // wx.request({
    //   url: 'https://selwyn.mynatapp.cc/user/findall', //仅为示例，并非真实的接口地址
    //   // data: {
    //   //   id: that.data.prodId
    //   // },
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   method: 'post',
    //   success: function (res) {

    //   }
    // })
    wx.request({
      url: 'https://selwyn.mynatapp.cc/prod/findSpec', //仅为示例，并非真实的接口地址
      // data: {
      //   id: that.data.prodId
      // },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'post',
      success: function (res) {
        console.log(res.data)
        var colorItems = []
        var specItems = []
        var colorarr = that.data.prod.color.split(',');
        console.log(colorarr)
        var specarr = that.data.prod.spec.split(',');
        // res.data.map(function(v,i){
        //   colorItems[i] = {
        //     name: v.color,
        //     value: v.color
        //   }
        //   if(colorarr.indexOf(v.color)>-1){
        //     console.log(i)
        //     colorItems[i].checked = 'true'
        //   }
        //   specItems[i] = {
        //     name: v.spec,
        //     value: v.spec
        //   }
        //   if (specarr.indexOf(v.spec) > -1) {
        //     console.log(i)
        //     specItems[i].checked = 'true'
        //   }
        // })
        colorarr.forEach(function (v, i) {
          colorItems[i] = {
            name: v,
            value: v
          }
        })
        specarr.forEach(function (v, i) {
          specItems[i] = {
            name: v,
            value: v
          }
        })
        that.setData({
          colorItems: colorItems,
          specItems: specItems,
          total: that.data.quan * that.data.prod.price
        })
      }
    })

  },
  colorChange: function (e) {
    this.data.attr[0] = e.detail.value
    // this.setData({
    //   attr: e.detail.value
    // })
  },
  specChange: function (e) {
    // this.setData({
    //   attr: e.detail.value
    // })
    this.data.attr[1] = e.detail.value
  },
  preOrder: function () {
    var a = this.data.attr
    console.log(a.length)
    var attrString = this.data.attr.join(',')
    if (a.length < 2) {
      wx.showToast({
        title: '没有选择完参数',
        icon: "none",
        duration: 2000
      })
    }
    else {
      var ct = new Date().getTime()
      var data = {
        total: this.data.quan * this.data.prod.price,
        quan: this.data.quan,
        attr: attrString,
        prodId: this.data.prod.id,
        prodName: this.data.prod.name,
        ct: ct + '',
        status: 0,
        icon: this.data.prod.icon,
        userId: index.data.userInfo.id
      }
      console.log(data)
      var that = this
      wx.request({
        url: 'https://selwyn.mynatapp.cc/order/insertOne', //仅为示例，并非真实的接口地址
        data: data,
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'post',
        success: function (res) {
          console.log(res.data)
          // updateDiscount
          var dis = that.data.dis - 1
          console.log(dis)
          // if(index.data.userInfo.pid == ''){
          //   console.log(index.data.userInfo.pid)
          //   dis = 0
          // }
          console.log(index.data.userInfo.id)
          var tho = that
          if (dis >= 0) {
            wx.request({
              url: 'https://selwyn.mynatapp.cc/user/updateDiscount', //仅为示例，并非真实的接口地址
              data: {
                discount: dis,
                id: index.data.userInfo.id
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              method: 'post',
              success: function (res) {
                tho.setData({
                  dis: dis
                })
              }
            })
          }
          console.log(Number(index.data.per.percentage1))
          console.log(Number(tho.data.total))
          if (index.data.userInfo.pid > 0 && dis > 0) {
            // console.log(tho.data.per.percentage1)
            wx.request({
              url: 'https://selwyn.mynatapp.cc/user/persent', //仅为示例，并非真实的接口地址
              data: {
                persent: index.data.per.percentage1 * tho.data.total * index.data.per.num,
                id: index.data.userInfo.pid
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              method: 'post',
              success: function (res) {
                console.log(res)
              }
            })
          }
          else if (index.data.userInfo.pid > 0 && dis == 0){
            wx.request({
              url: 'https://selwyn.mynatapp.cc/user/persent', //仅为示例，并非真实的接口地址
              data: {
                persent: index.data.per.percentage1 * tho.data.total,
                id: index.data.userInfo.pid
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              method: 'post',
              success: function (res) {
                console.log(res)
              }
            })
          }
          else if (index.data.userInfo.pid < 0) {
            
          }
          // wx.request({
          //   url: 'https://selwyn.mynatapp.cc/user/updateDiscount', //仅为示例，并非真实的接口地址
          //   data: {
          //     discount:dis-1
          //   },
          //   header: {
          //     'content-type': 'application/json' // 默认值
          //   },
          //   method: 'post',
          //   success: function (res) {

          //   }
          // })
          if (res.data.msg != 'msg') {
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000
            })
          }
          else {
            that.setData({ showView: 1 });
            var fadeOutTimeout = setTimeout(() => {
              that.setData({ showView: 0 });
              clearTimeout(fadeOutTimeout);
            }, 2000);
          }
        }
      })
    }
  }
})  